package org.geektimes.web.mvc.servlet;

import org.geektimes.projects.microprofile.servlet.ApplicationContextListener;
import org.geektimes.web.mvc.FrontControllerServlet;

import javax.annotation.Priority;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @author biwin
 */
@Priority(3)
public class MvcCoreServletListener implements ApplicationContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        servletContext.log("Priority : 3 (FrontControllerServlet)");

        try {
            FrontControllerServlet servlet = servletContext.createServlet(FrontControllerServlet.class);
            ServletRegistration.Dynamic frontControllerServlet = servletContext.addServlet("FrontControllerServlet",
                    servlet);
            frontControllerServlet.addMapping("/");
            frontControllerServlet.setLoadOnStartup(1);
//            servlet.init(servlet);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
